# GF_PY3_FUNC_MEMO_AI_App_Program_Interface.py
# Create by GF 2025-05-05 11:38

import requests

# ##################################################

# OpenAI API Standard (20250505):
# - {"Comment": "Spark4.0 Ultra", "Model": "4.0Ultra", "URL": "https://spark-api-open.xf-yun.com/v1/chat/completions"}
# - {"Comment": "Spark Lite",     "Model": "lite",     "URL": "https://spark-api-open.xf-yun.com/v1/chat/completions"}

# ##################################################
# Basic Functions

def Requests_POST_Ollama_0_6_API_Chat(URL:str, Model:str, Message_List:list) -> dict:

    # Message_List 应包含:
    # [
    #     {"role":      "user", "content": "Hello!"},
    #     {"role": "assistant", "content": "Hello, I am an AI assistant!"},
    #     {"role":      "user", "content": "What time is it?"},
    #     ......
    # ]
    # ..............................................
    # URL Similar to: http://127.0.0.1:11434/api/chat
    # ..............................................
    # 调用 Ollama 模型生成回复
    Rqs_JSON = {"model": Model, "messages":Message_List, "stream":False}
    # ..............................................
    Response = requests.post(url=URL, json=Rqs_JSON)
    # ..............................................
    Rsp_JSON = Response.json()
    # ..............................................
    # 获取模型响应:
    # - Rsp_Message = Rsp_JSON.get("message", {})
    # - Rsp_Content = Rsp_Message["content"]
    return Rsp_JSON

def Requests_POST_OpenAI_20250505_API_V1_Chat_Completions(URL:str, Key:str, Model:str, Message_List:list) -> dict:

    # Message_List 应包含:
    # [
    #     {"role":    "system", "content": "Role setting: You are a knowledgeable assistant"},
    #     {"role":      "user", "content": "Hello!"},
    #     {"role": "assistant", "content": "Hello, I am an AI assistant!"},
    #     {"role":      "user", "content": "What time is it?"},
    #     ......
    # ]
    # ..............................................
    # URL Similar to: http://127.0.0.1:3001/v1/chat/completions
    # ..............................................
    # 调用 OpenAI 模型生成回复
    Rqs_Headers = {"Authorization": "Bearer %s" % Key, "Content-Type": "application/json"}
    Rqs_JSON    = {"model": Model, "messages": Message_List, "stream": False, "temperature": 0.7}
    # ..............................................
    Response = requests.post(URL, headers=Rqs_Headers, json=Rqs_JSON)
    Rsp_JSON = Response.json()
    # ..............................................
    # 获取模型响应:
    # - Rsp_Choices = Rsp_JSON.get("choices", [])
    # - Rsp_Message = Rsp_Choices[0]["message"]
    # - Rsp_Content = Rsp_Message["content"]
    # ..............................................
    return Rsp_JSON

# EOF Signed by GF.
